package com.campus.service.Impl;

import com.campus.dao.UserDao;
import com.campus.pojo.Pager;
import com.campus.pojo.SysModule;
import com.campus.pojo.SysUser;
import com.campus.service.UserService;
import com.campus.tools.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("userService")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Override
    public SysUser login(String name, String psw) {
        Map<String,Object>params = new HashMap<>();
        params.put("name",name);
        params.put("psw",psw);
        return userDao.selectByNameAndPwd(params);
    }

    @Override
    public List<SysModule> selectModuleById(String userId) {
        return userDao.selectModuleById(userId);
    }

    @Override
    public int count(Map<String, Object> params) {
        return userDao.count(params);
    }

    @Override
    public List<SysUser> selectAreaUserByPage(SysUser user, Pager pager) {
        Map<String,Object> params = new HashMap<>();
        params.put("user",user);
        params.put("pager",pager);
        //System.out.println(userDao.selectAreaUserByPage(params));//调试
        return userDao.selectAreaUserByPage(params);
    }

    @Override//检验用户是否存在
    public String isExist(SysUser user) {
        SysUser user1 = userDao.isExist(user);//找出被编辑的用户
        //System.out.println(user1);//调试
        if (user1 != null && user1.getUserCode() != null ){ //编辑
            if (userDao.isExistAdmin(user)>0 && !user.getAreaId().equals(user1.getAreaId())){//如果该区域已存在管理员 排除只改名的情况
                return "existAdmin";
            }
            if ( userDao.duplicateName(user1)>1 )//如果重名
                return "exist";
            return "no";
        }else { //新增
            if ( userDao.duplicateName(user)>0 )//如果重名
                return "exist";
            if ( userDao.isExistAdmin(user)>0 )//如果该区已有管理员
                return "existAdmin";
            return "no";
        }
    }

    @Override
    public int upAreaAdmin(SysUser user) {
        return userDao.upAreaAdmin(user);
    }

    @Override
    public int addAreaAdmin(SysUser user) {
        user.setUserCode(UUIDGenerator.getUUID());
        user.setDelState(1);
        return userDao.addAreaAdmin(user);
    }

    @Override
    public int deleteAreaUser(SysUser user) {
        return userDao.deleteAreaUser(user);
    }

    @Override
    public int resetPassword(SysUser user) {
        return userDao.resetPassword(user);
    }

    @Override
    public SysUser selectByUserCode(String code) {
        return userDao.selectByUserCode(code);
    }

    @Override
    public Integer count1(Map<String, Object> params) {
        return userDao.count1(params).size();
    }

    @Override
    public List<SysUser> selectByPage1(SysUser user, Pager pager) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("user", user);
        int recordCount = userDao.count1(params).size();
        pager.setRowCount(recordCount);
        if (recordCount > 0) {
            params.put("pager", pager);
        }
        return userDao.selectByPage1(params);
    }

    @Override
    public String isExistName(String name, String userCode, String areaId) {
        String result;
        int i = userDao.isExistName(name, userCode, areaId);
        if (i > 0) {
            result = "exit";
        } else {
            result = "no";
        }
        return result;
    }

    @Override
    public String isExistUnit(String unitId, String userCode, String areaId) {
        String result;
        int i = userDao.isExistUnit(unitId, userCode, areaId);
        if (i > 0) {
            result = "exitUnit";
        } else {
            result = "noUnit";
        }
        return result;
    }

    @Override
    public int upUser(SysUser user) {
        return userDao.upUser(user);
    }

    @Override
    public List<String> getCheckedRole(String userCode) {
        return userDao.getCheckedRole(userCode);
    }

    @Override
    public int bindRole(String userCode, String roleCodes, String areaId) {
        if (roleCodes.length() > 0) {
            roleCodes = roleCodes.substring(0, roleCodes.length() - 1);
        }
        String[] roleCode = roleCodes.split(",");
        int resulta = userDao.delRoleByUserId(userCode);
        if ((resulta > 0) && (roleCode == null || roleCodes.isEmpty())) {// 当给某个角色删除全部权限时
            return 1;
        }
        int result = 0;
        for (int i = 0; i < roleCode.length; i++) {
            result = userDao.bindRoleByUserId(UUIDGenerator.getUUID(), userCode, roleCode[i], areaId);
        }
        return result;
    }
}
